package org.quipu.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import org.quipu.client.events.AppEvent;
import org.quipu.client.events.AppEventBus;
import org.quipu.client.events.AppEventListener;
import org.quipu.client.events.GotoPageEvent;
import static org.quipu.client.events.GotoPageEvent.Page.SETTINGS;
import org.quipu.client.events.LoginEvent;
import org.quipu.dto.UserDto;

public class MainWindow extends AppEventWidget implements AppEventListener {

    private final VerticalPanel vPanel;
    private AppEventWidget centralPanel;
    HorizontalPanel errorPanel;
    private Button errorButton;
    private ErrorWidget errorWidget;
    private AccountsPanel accountsPanel;
    private TransactionsPanel transactionsPanel;
    private SettingsPanel settingsPanel;
    private ReportsPanel reportsPanel;
    private LoginPanel loginPanel;

    public MainWindow(AppEventBus eventBus, AppCache cache) {
        super(eventBus, cache);
        vPanel = new VerticalPanel();
        vPanel.setWidth("100%");
//        vPanel.setSize("100%", "100%");
    }

    @Override
    public void init() {
        getMainService().getSessionUser(new AsyncCallback<UserDto>() {

            public void onSuccess(UserDto user) {
                initUi(user);
                getEventBus().addListener(LoginEvent.class, MainWindow.this);
                getEventBus().addListener(GotoPageEvent.class, MainWindow.this);
            }

            public void onFailure(Throwable caught) {
                Window.alert(caught.getMessage());
            }
        });
    }

    private void initUi(UserDto user) {
        getCache().init(user);
        accountsPanel = new AccountsPanel(getEventBus(), getCache());
        accountsPanel.init();

        transactionsPanel = new TransactionsPanel(getEventBus(), getCache());
        transactionsPanel.init();

        settingsPanel = new SettingsPanel(getEventBus(), getCache());
        settingsPanel.init();

        reportsPanel = new ReportsPanel(getEventBus(), getCache());
        reportsPanel.init();

        loginPanel = new LoginPanel(getEventBus(), getCache());
        loginPanel.init();

        HeaderPanel headerPanel = new HeaderPanel(getEventBus(), getCache());
        headerPanel.init();
        vPanel.add(headerPanel.getWidget());

        errorWidget = new ErrorWidget(getEventBus(), getCache());
        errorWidget.init();
        errorButton = new Button();
        errorButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                toggleErrorWidget();
            }
        });
        errorPanel = new HorizontalPanel();
        errorPanel.add(errorButton);
        errorPanel.add(errorWidget.getWidget());
        vPanel.add(errorPanel);
        toggleErrorWidget();

        getEventBus().fireEvent(new GotoPageEvent(this, GotoPageEvent.Page.ACCOUNTS));
        getEventBus().fireEvent(new LoginEvent(this, user));
    }

    @Override
    public void handleEvent(AppEvent event) {
        if(event != null) {
            if(event instanceof LoginEvent) {
                if(getCache().getUser() != null)
                    getEventBus().fireEvent(new GotoPageEvent(this, GotoPageEvent.Page.ACCOUNTS));
                else
                    getEventBus().fireEvent(new GotoPageEvent(this, GotoPageEvent.Page.LOGIN));
            } else if(event instanceof GotoPageEvent) {
                if(centralPanel != null) {
                    vPanel.remove(centralPanel.getWidget());
                }
                HasHorizontalAlignment.HorizontalAlignmentConstant hAlign = HasHorizontalAlignment.ALIGN_LEFT;
                switch(((GotoPageEvent) event).getObject()) {
                    case ACCOUNTS:      centralPanel = accountsPanel;
                                        break;
                    case TRANSACTIONS:  centralPanel = transactionsPanel;
                                        break;
                    case SETTINGS:      centralPanel = settingsPanel;
                                        break;
                    case REPORTS:       centralPanel = reportsPanel;
                                        break;
                    case LOGIN:         centralPanel = loginPanel;
                                        hAlign = HasHorizontalAlignment.ALIGN_CENTER;
                                        break;
                    case USER_PROFILE:
                                        UserProfilePanel userPanel = new UserProfilePanel(getEventBus(), getCache());
                                        userPanel.init();
                                        centralPanel = userPanel;
                                        break;
                    default:            centralPanel = null;
                                        break;
                }
                if(centralPanel != null) {
                    vPanel.insert(centralPanel.getWidget(), 1);
                    vPanel.setCellHorizontalAlignment(centralPanel.getWidget(), hAlign);
                }
            }
        }
    }

    @Override
    public Widget getWidget() {
        return vPanel;
    }

    private void toggleErrorWidget() {
        if(errorPanel.getWidgetCount() > 1) {
            errorPanel.remove(errorWidget.getWidget());
            errorButton.setText("Show errors");
        } else {
            errorPanel.add(errorWidget.getWidget());
            errorButton.setText("Hide errors");
        }
    }
}
